/*
 * Copyright (c) 2025 GuoKaiHong (Shenzhen) Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import { initCallback } from "./i_id_card_service";
import { sayHelloCallback } from "./i_id_card_service";
import { selfCheckCallback } from "./i_id_card_service";
import { idc_card_searchCallback } from "./i_id_card_service";
import { idc_card_selectCallback } from "./i_id_card_service";
import { fetch_card_infoCallback } from "./i_id_card_service";
import { gkh_identity_card_closeCallback } from "./i_id_card_service";
import IIdCardService from "./i_id_card_service";
import rpc from "@ohos.rpc";

export default class IdCardServiceProxy implements IIdCardService {
  constructor(proxy) {
    this.proxy = proxy;
  }

  init(deviceId: number, callback: initCallback): void {
    let _option = new rpc.MessageOption();
    let _data = new rpc.MessageParcel();
    let _reply = new rpc.MessageParcel();
    _data.writeInt(deviceId);
    this.proxy.sendRequest(IdCardServiceProxy.COMMAND_INIT, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        callback(_errCode);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  sayHello(str: string, callback: sayHelloCallback): void {
    let _option = new rpc.MessageOption();
    let _data = new rpc.MessageParcel();
    let _reply = new rpc.MessageParcel();
    _data.writeString(str);
    this.proxy.sendRequest(IdCardServiceProxy.COMMAND_SAY_HELLO, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readString();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  selfCheck(callback: selfCheckCallback): void {
    let _option = new rpc.MessageOption();
    let _data = new rpc.MessageParcel();
    let _reply = new rpc.MessageParcel();
    this.proxy.sendRequest(IdCardServiceProxy.COMMAND_SELF_CHECK, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        callback(_errCode);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  idc_card_search(callback: idc_card_searchCallback): void {
    let _option = new rpc.MessageOption();
    let _data = new rpc.MessageParcel();
    let _reply = new rpc.MessageParcel();
    this.proxy.sendRequest(IdCardServiceProxy.COMMAND_IDC_CARD_SEARCH, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  idc_card_select(callback: idc_card_selectCallback): void {
    let _option = new rpc.MessageOption();
    let _data = new rpc.MessageParcel();
    let _reply = new rpc.MessageParcel();
    this.proxy.sendRequest(IdCardServiceProxy.COMMAND_IDC_CARD_SELECT, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readInt();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  fetch_card_info(callback: fetch_card_infoCallback): void {
    let _option = new rpc.MessageOption();
    let _data = new rpc.MessageParcel();
    let _reply = new rpc.MessageParcel();
    this.proxy.sendRequest(IdCardServiceProxy.COMMAND_FETCH_CARD_INFO, _data, _reply, _option).then(function (result) {
      if (result.errCode === 0) {
        let _errCode = result.reply.readInt();
        if (_errCode != 0) {
          let _returnValue = undefined;
          callback(_errCode, _returnValue);
          return;
        }
        let _returnValue = result.reply.readString();
        callback(_errCode, _returnValue);
      } else {
        console.log("sendRequest failed, errCode: " + result.errCode);
      }
    })
  }

  gkh_identity_card_close(callback: gkh_identity_card_closeCallback): void {
    let _option = new rpc.MessageOption();
    let _data = new rpc.MessageParcel();
    let _reply = new rpc.MessageParcel();
    this.proxy.sendRequest(IdCardServiceProxy.COMMAND_gkh_IDENTITY_CARD_CLOSE, _data, _reply, _option)
      .then(function (result) {
        if (result.errCode === 0) {
          let _errCode = result.reply.readInt();
          if (_errCode != 0) {
            let _returnValue = undefined;
            callback(_errCode, _returnValue);
            return;
          }
          let _returnValue = result.reply.readInt();
          callback(_errCode, _returnValue);
        } else {
          console.log("sendRequest failed, errCode: " + result.errCode);
        }
      })
  }

  static readonly COMMAND_INIT = 1;
  static readonly COMMAND_SAY_HELLO = 2;
  static readonly COMMAND_SELF_CHECK = 3;
  static readonly COMMAND_IDC_CARD_SEARCH = 4;
  static readonly COMMAND_IDC_CARD_SELECT = 5;
  static readonly COMMAND_FETCH_CARD_INFO = 6;
  static readonly COMMAND_gkh_IDENTITY_CARD_CLOSE = 7;
  private proxy
}

